package com.jd.onlinequery.common.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by liusy on 2018/5/23.
 */
public class MySelenium {


    private final static String TARGET_URL = "http://you.ctrip.com/sight/Chongqing158/50223.html";
    private final static String FILE_NAME = "/Users/abc123/Documents/workspace/gitosc/jd-onlinequery/SinaSportsNews.txt";

    private static  StringBuilder allNewses = new StringBuilder();

    public static void main(String[] args) {
        try {
            WebDriver driver = MyWebDriver.createWebDriver();
            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
            driver.get(TARGET_URL);
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until(ExpectedConditions.elementToBeClickable(By.className("nextpage")));
            Thread.sleep(1000*10);
            parseHtml(driver.getPageSource());
            for (int i=0;i<=390;i++) {

                wait.until(ExpectedConditions.elementToBeClickable(By.className("nextpage")));
                Actions action = new Actions(driver);
                WebElement we = driver.findElement(By.className("comment_entrance"));
                action.moveToElement(we).build().perform();

                driver.findElement(By.className("nextpage")).click();
                Thread.sleep(1000 *3 );
                parseHtml(driver.getPageSource());
            }

            driver.quit();

            MyFileWriter.writeString(FILE_NAME, allNewses.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public  static  void parseHtml(String pageSource)
    {
        Document document = Jsoup.parse(pageSource);
        List<Element> liTags = document.getElementsByAttributeValue("itemprop","description");
        for (int i = 0; i < liTags.size(); i++) {
            List<Element> elements =liTags.get(i).getElementsByClass("heightbox");
            if(null!=elements && elements.size()>0) {
                String source = liTags.get(i).getElementsByClass("heightbox").get(0).text();
                //allNewses.append(title + "\n");
                System.out.println(source);
                allNewses.append("" + source + "\n");
            }

        }
    }


}


class MyFileWriter {

    public static void writeString(String fileName, String content) {
        FileWriter fileWriter = null;
        try {
            File file = new File(fileName);
            if (!file.exists()) file.createNewFile();

            fileWriter = new FileWriter(file);
            fileWriter.write(content);
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


class MyWebDriver {
    private final static String CHROME_DRIVER_PATH = "/Users/abc123/Documents/workspace/gitosc/jd-onlinequery/chromedriver";

    public static WebDriver createWebDriver() {
        System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
        return new ChromeDriver();
    }
}